<?php
require_once('../../includes/framework_ajax.php');

@$oper = $_POST['oper'];
@$page = $_GET['page']; // get the requested page
@$limit = $_GET['rows']; // get how many rows we want to have into the grid
@$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
@$sord = $_GET['sord']; // get the direction
@$id = $_GET['id'];
@$cd_grupo = $_GET['form'];
@$ins = $_GET['ins'];
@$nd = $_GET['nd'];

$user = Session::get('login');
$ci_usuario = $user['ci_usuario'];

if($cd_grupo == 0){
	if(@$id && @$ins == 'inserir'){
		$sqlNum = "select nextval('tethys.tb_grupo_ci_grupo_seq') as num;";
		$queryNum = Connection::query($sqlNum);
		$rowNum = $queryNum->fetch();
		$cd_grupo = $rowNum['num'];

		$insert = "insert into tethys.tb_grupotransacoes
				(cd_grupo, cd_transacao, fl_inserir, fl_alterar, fl_deletar, dt_criacao, cd_usuario_insert)
				values ($cd_grupo, $id,'N','N','N', now(), '$ci_usuario')";
		echo $insert;
		//Connection::exec($insert);

		if(!$sidx) $sidx =1; // connect to the database
		$sql = 'select tgt.ci_grupotransacoes, tt.ci_transacao, tt.nm_transacao, tgt.fl_inserir, tgt.fl_alterar, tgt.fl_deletar
				from tethys.tb_grupotransacoes tgt
				inner join tethys.tb_modulotransacao tmt on (tgt.cd_transacao = tmt.cd_transacao)
				inner join tethys.tb_transacao tt on (tt.ci_transacao = tmt.cd_transacao)
				where tmt.cd_modulo = '.Config::MODULO.'
				and tgt.cd_grupo = '.$cd_grupo;
	}
}else{
	//inserir registro
	if(@$id && @$ins == 'inserir'){
		$insert = "insert into tethys.tb_grupotransacoes
				(cd_grupo, cd_transacao, fl_inserir, fl_alterar, fl_deletar, dt_criacao, cd_usuario_insert)
				values ($cd_grupo, $id,'N','N','N', now(), '$ci_usuario')";
		Connection::exec($insert);
	}

	if(!$sidx) $sidx =1; // connect to the database
	$sql = 'select tgt.ci_grupotransacoes, tt.ci_transacao, tt.nm_transacao, tgt.fl_inserir, tgt.fl_alterar, tgt.fl_deletar
				from tethys.tb_grupotransacoes tgt
				inner join tethys.tb_modulotransacao tmt on (tgt.cd_transacao = tmt.cd_transacao)
				inner join tethys.tb_transacao tt on (tt.ci_transacao = tmt.cd_transacao)
				where tmt.cd_modulo = '.Config::MODULO.'
				and tgt.cd_grupo = '.$cd_grupo;
}

$rs = Connection::query($sql);
$count = $rs->rowCount();

if( $count >0 ) {
	$total_pages = ceil($count/$limit);
} else {
	$total_pages = 0;
}

if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;

$sql .= " ORDER BY $sidx $sord offset $start limit $limit;"; //LIMIT $start , $limit
$rs = Connection::query($sql);

@$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;

$i=0;
while($row = $rs->fetch()){
	$fl_inserir = (($row['fl_inserir'] == 'S')? 'S' : 'N');
	$fl_alterar = (($row['fl_alterar'] == 'S')? 'S' : 'N');
	$fl_deletar = (($row['fl_deletar'] == 'S')? 'S' : 'N');
	$responce->rows[$i]['ci'] = $row['ci_transacao'];
	$responce->rows[$i]['id'] = $row['ci_grupotransacoes'];
	$responce->rows[$i]['cell'] = array($row['ci_transacao'],
	$row['nm_transacao'], $fl_inserir,
	$fl_alterar, $fl_deletar);
	$i++;
}
echo json_encode($responce);
?>